﻿
 @inject IconService iconService

<div style="width: 256px;">
    <Button Type="primary" OnClick="ToggleCollapsed" Style="margin-bottom: 16px">
        @if (collapsed)
        {
            <Icon Type="menu-unfold" Theme="outline"></Icon>
        }
        else
        {
            <Icon Type="menu-fold" Theme="outline"></Icon>
        }
    </Button>
    <Menu 
          Mode=MenuMode.Inline
          Theme=MenuTheme.Dark
          InlineCollapsed=collapsed>
        <T_Menu MenuItems='ConvertToTree(MulV, "0")' />
        
      
    </Menu>
</div>
@code {
    	bool collapsed = false;


	void ToggleCollapsed()
	{
		collapsed = !collapsed;
	}

	protected override async Task OnAfterRenderAsync(bool firstRender)
	{
		if (firstRender)
		{
			await iconService.CreateFromIconfontCN("//at.alicdn.com/t/font_2735473_hi62ezq5579.js");
		}
	}


    public static List<Mulu> MulV = new List<Mulu>
{
    new Mulu { ID = "1", Sort = "1", ParentNode = "0", Text = "首页", URL = "#" },
        new Mulu { ID = "2", Sort = "2", ParentNode = "0", Text = "测试1", URL = "#" },
        new Mulu { ID = "3", Sort = "3", ParentNode = "0", Text = "首页1", URL = "#" },
        new Mulu { ID = "4", Sort = "4", ParentNode = "0", Text = "测试1", URL = "#" },
        new Mulu { ID = "5", Sort = "1", ParentNode = "1", Text = "首页2", URL = "/" },
        new Mulu { ID = "6", Sort = "2", ParentNode = "1", Text = "测试2", URL = "/cou" },
        new Mulu { ID = "7", Sort = "1", ParentNode = "2", Text = "首页3", URL = "/" },
        new Mulu { ID = "8", Sort = "1", ParentNode = "3", Text = "测试3", URL = "/1" },
        new Mulu { ID = "9", Sort = "3", ParentNode = "1", Text = "首页2", URL = "/" },
        new Mulu { ID = "10", Sort = "2", ParentNode = "2", Text = "测试3", URL = "/cer" },
        new Mulu { ID = "11", Sort = "3", ParentNode = "2", Text = "首页3", URL = "/" },
        new Mulu { ID = "12", Sort = "2", ParentNode = "3", Text = "测试4", URL = "/couter" },

        new Mulu { ID = "8", Sort = "1", ParentNode = "5", Text = "测试5", URL = "/cter" },
        new Mulu { ID = "9", Sort = "3", ParentNode = "5", Text = "首页5", URL = "/" },
        new Mulu { ID = "10", Sort = "2", ParentNode = "5", Text = "测试5", URL = "/cnter" },
        new Mulu { ID = "11", Sort = "3", ParentNode = "6", Text = "首页6", URL = "/" },
        new Mulu { ID = "12", Sort = "2", ParentNode = "6", Text = "测试6", URL = "/counter" },
};

    public static List<BlazorDemo.Data.TreeNode> ConvertToTree(List<Mulu> list, string parentNode)
    {
        var rootNodes = list.Where(x => x.ParentNode == parentNode)
                            .Select(x => new BlazorDemo.Data.TreeNode
                                {
                                    Node = x,
                                    Children = ConvertToTree(list, x.ID)
                                })
                            .ToList();
        return rootNodes;
    }
    private bool collapseNavMenu = true;

    private string? NavMenuCssClass => collapseNavMenu ? "collapse" : null;

    private void ToggleNavMenu()
    {
        collapseNavMenu = !collapseNavMenu;
    }
    public class Mulu
    {

        public string? ID { get; set; }
        public string? Sort { get; set; }
        public string? ParentNode { get; set; }
        public string? Text { get; set; }
        public string? URL { get; set; }
        public string? ICO { get; set; }
        public string? Class { get; set; }
        public bool Expanded { get; set; }
    }
}